
#include "rstatd.h"

Err rp_rstatd_getopt(int argc, char *argv[])
{
	Err err;

	//err = rp_getopt_common(argc, argv, optstring, argarray [ ], argcount);

	return err;
}

void rp_rstatd_env_init()
{
	struct stat envstat;
	
	if ((env.cfgdir = getenv("RP_CFG_DIR")))
	{
		if (stat(env.cfgdir, &envstat) < 0) 
		{
			env.cfgdir = "/etc/rally/bin";
		}		
	} else {
		env.cfgdir = "/etc/rally/bin";
	}
	if (stat(env.cfgdir, &envstat) < 0) 
	{
		exit(127);
	}
	
	if ((env.bindir = getenv("RP_BIN_DIR")))
	{
		if (stat(env.bindir, &envstat) < 0) 
		{
			env.bindir = "/etc/rally/bin";
		}		
	} else {
		env.bindir = "/etc/rally/bin";
	}
	if (stat(env.bindir, &envstat) < 0) 
	{
		exit(127);
	}	
}

void rp_rstatd_config_init()
{
	/* init cfgv array  */
	config.cfgc = 10;
	config.cfgv = (KeyValue*)calloc(config.cfgc, sizeof(KeyValue));
	config.cfgv[0].key = "LogDir";
	config.cfgv[1].key = "LogLevel";
	config.cfgv[2].key = "ChildPrefork";
	config.cfgv[3].key = "ChildMin";
	config.cfgv[4].key = "ChildIdle";
	config.cfgv[5].key = "HeartbeatTimer";
	config.cfgv[6].key = "RallyDir";
	config.cfgv[7].key = "RindexdHostname";
	config.cfgv[8].key = "RindexdIPAddress";
	config.cfgv[9].key = "RindexdPortNum";

	/*	read from config file   */
	if (rp_config_init(env.cfgdir, "rstatd", config.cfgv, config.cfgc) == E_FATAL)  exit(127);
	
	/*	convert from string to real type   */
	config.logdir = ((config.cfgv[0].value == NULL) ? "/tmp/rally/" : strdup(config.cfgv[0].value));
	if (config.cfgv[1].value == NULL)
	{
		config.loglv = LOG_INFO;
	}
	else {
		int i;
		for (i=0; i<5; i++)
		{
			if (strcmp(config.cfgv[1].value, log_level_array[i]) == 0)
			{
				config.loglv = (Log_Level)i;
				break;
			}
		}
		if (config.loglv == 0)  config.loglv = LOG_INFO;
	}
	config.num_child_prefork = (config.cfgv[2].value == NULL) ? 10 : atoi(config.cfgv[2].value);
	config.num_child_min = (config.cfgv[3].value == NULL) ? 4 : atoi(config.cfgv[3].value);
	config.num_child_idle = (config.cfgv[4].value == NULL) ? 2 : atoi(config.cfgv[4].value);
	memset(&(config.tv_heartbeat), 0, sizeof(struct timeval));
	config.tv_heartbeat.tv_sec = (config.cfgv[5].value == NULL) ? 2 : atoi(config.cfgv[5].value);
	config.rpdir = (config.cfgv[6].value == NULL) ? "/etc/rally/" : strdup(config.cfgv[6].value);
	if (config.cfgv[7].value != NULL) config.hostname_rindexd = strdup(config.cfgv[7].value);
	if (config.cfgv[8].value != NULL) 
	{	/*	Rindexd's IP has been explicitly specified	*/
		config.addr_rindexd.sin_addr.s_addr = inet_addr(config.cfgv[8].value);
	} else {
		/*	get IP address from Rindexd's hostname	*/
		// TODO:
	}
	config.addr_rindexd.sin_port = (config.cfgv[9].value == NULL) ? htons(8741) : htons((uint16_t)atoi(config.cfgv[9].value));
	config.addr_rindexd.sin_family = AF_INET;
	memset(&config.addr_rindexd.sin_zero, 0, 8);
	
	/*	free mem  */
	int i;
	for (i=0; i<config.cfgc; i++)
	{
		free(config.cfgv[i].value);
	}
	free(config.cfgv);
	return;
}

void rp_rstatd_log_init()
{
	char fname[] = "rp_rstatd_log_init";
	char msg[] = "fatal error";
	Err err;

	if (rp_log_init(config.logdir, "rstatd", config.loglv) != E_OK) exit(127);	
}

void rp_rstatd_global_init()
{
	char fname[] = "rp_rstatd_global_init";
	char msg[] = "fatal error";
	Err err;

	err = rp_rstatd_scoreboard_init(&global.sb_live);
	err = rp_rstatd_scoreboard_init(&global.sb_dead);
	
	return;
}

